﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Text
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        public Form3(int id,string name,int age,string socre)
        {
            InitializeComponent();
            this.id = id;
            textName.Text = name;
            textAge.Text = age.ToString();
            textScore.Text = socre;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private int id;


        //确认保存
        private void button1_Click(object sender, EventArgs e)
        {
            var ageString = textAge.Text.ToString();
            var scoreString = textScore.Text.ToString();

            var name = textName.Text;
            var age = ageString.Count() > 0 ? int.Parse(ageString) : 18;
            var score = scoreString.Count() > 0 ? int.Parse(scoreString) : 0;

            //更新
            if (id > 0)
            {
                if(!string.IsNullOrEmpty(name))
                {
                    var sql1 = string.Format("update Students set Name='{0}',Age={1},Scroe={2} Where Id={3}", name, age, score, this.id);
                    var res = DbHelp.AddOrEditOrDeleteSave(sql1);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {
                if(!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Students(Name,Age,Scroe)values('{0}','{1}','{2}')", name, age, score);

                    var res = DbHelp.AddOrEditOrDeleteSave(sql);

                    
                    
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    //else
                    //{
                    //    MessageBox.Show("添加失败");
                    //    this.Close();
                    //}

                }
                else
                {
                    MessageBox.Show("学生信息不为空");
                }
            }
        }

        private void textRePwd_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textPassWord_TextChanged(object sender, EventArgs e)
        {

        }

        private void textUserName_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.No;
            this.Close();
        }
    }
}
